/*
 * TestAI.cpp
 *
 *  Created on: Nov 4, 2008
 *      Author: Ryan
 */

#include "TAI.h"

#include "ResourceAnalyzer.h"
#include "UnitAnalyzers/BuilderExtractor.h"
#include "UnitAnalyzers/FactoryFilter.h"
#include "UnitAnalyzers/ResourceProducerUnitVisitor.h"
#include "Tools/WorkPool.h"
#include "Experts/BuilderExpert.h"
#include "Experts/BuildingExpert.h"


#include "../../OOInterface/Objects/Units/UnitAspects/BuilderAspect.h"
#include "../../OOInterface/Objects/Events/UnitIdleEvent.h"

#include "../../OOInterface/Objects/Logger.h"

#include <vector>
#include <sstream>
#include <list>
#include <exception>

TAI::TAI( OOInterface::AIInterface* i ):
	AIHandle(i), _frame(0)
{
}

TAI::~TAI()
{
}

void TAI::init( OOInterface::ConstPlayerPtr self )
{
	getAIInterface()->addGameListener( this );

	_self = self;

	//OOInterface::Logger::getInstance("AI.txt") << _self->toString() << std::endl;
}

void TAI::update()
{
	++_frame;
}

void TAI::run()
{
	//WorkPool pool(5);
	//pool.startWorkers();

	std::list< ResourceAnalysisUnit* > resources;

	ResourceReservation reserve;
	BuildingExpert expert( getAIInterface(), _self, &reserve );


	while( true )
	{
		try {

			reserve.updateTimers();

			expert.run();

			boost::this_thread::yield();
		}
		catch( ... )
		{
			OOInterface::Logger::getInstance("AI.txt") << "Exception thrown in TAI::run" << std::endl;
		}
	}
}

void TAI::handle( OOInterface::ConstGameEventPtr e )
{

}
